Skip to main content

std.list

Pebble 0.3.1 · all symbols on this page are stable.

Polymorphic helpers over List<T>. Everything here is also available as a method (xs.length(), xs.map(f), etc.) — use the namespace form when you need to pass the helper itself as a value, e.g. std.list.some(std.int.isZero, xs).

See List<T> for the method-call surface (which additionally includes map, intentionally omitted here — see note at the bottom).

Methods

FunctionDescription
length<T>(xs: List<T>): intNumber of elements. Walks the list — O(n).
isEmpty<T>(xs: List<T>): boolTrue iff xs has zero elements.
head<T>(xs: List<T>): TFirst element. Fails on empty.
tail<T>(xs: List<T>): List<T>Everything after the head. Fails on empty.
prepend<T>(x: T, xs: List<T>): List<T>New list with x at the front.
drop<T>(n: int, xs: List<T>): List<T>List with the first n elements removed. Returns empty if n >= length.
foldr<T, A>(f: (T, A) -> A, init: A, xs: List<T>): ARight fold. foldr(f, z, [a, b, c]) = f(a, f(b, f(c, z))).
foldl<T, A>(f: (A, T) -> A, init: A, xs: List<T>): ALeft fold. Tail-recursive; preferred for large lists.
filter<T>(p: (T) -> bool, xs: List<T>): List<T>Keep elements where p returns true.
some<T>(p: (T) -> bool, xs: List<T>): boolTrue iff any element satisfies p. Short-circuits.
every<T>(p: (T) -> bool, xs: List<T>): boolTrue iff all elements satisfy p. Short-circuits.
find<T>(p: (T) -> bool, xs: List<T>): Optional<T>First matching element wrapped in Some, or None.
equals<T>(eq: (T, T) -> bool, a: List<T>, b: List<T>): boolElement-wise equality using eq.
map is method-only

xs.map(f) is available as a method on every List<T>, but is intentionally not exposed in std.list. The namespace-level form would need TIR-level composition (constructing a nil literal of the target element type) that the namespace helpers don't currently support. Keep using xs.map(f).

Examples

using {
length, isEmpty, head, tail, prepend, drop,
foldr, foldl, filter, some, every, find, equals
} = std.list;

const xs: List<int> = [1, 2, 3, 4, 5];

const n: int = length(xs); // 5
const e: bool = isEmpty(xs); // false
const h: int = head(xs); // 1
const rest: List<int> = tail(xs); // [2,3,4,5]
const cons: List<int> = prepend(0, xs); // [0,1,2,3,4,5]
const dr: List<int> = drop(2, xs); // [3,4,5]
const sumR: int = foldr((a, acc) => a + acc, 0, xs);// 15
const sumL: int = foldl((acc, a) => acc + a, 0, xs);// 15
const ev: List<int> = filter((n) => n % 2 == 0, xs); // [2,4]
const any: bool = some((n) => n > 4, xs); // true
const all: bool = every((n) => n > 0, xs); // true
const got: Optional<int>= find((n) => n == 3, xs); // Some{ value: 3 }
const same: bool = equals(std.int.equals, xs, xs); // true

See also